package com.minimalist.lindi.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.minimalist.common.entity.lindi.po.LindiPurchaseApplicationTownItem;
import com.minimalist.common.entity.lindi.vo.purchaseapplicationtownitem.LindiPurchaseApplicationTownItemVO;
import com.minimalist.lindi.mapper.LindiPurchaseApplicationTownItemMapper;
import com.minimalist.lindi.service.LindiPurchaseApplicationTownItemService;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.minimalist.common.entity.lindi.po.table.LindiPurchaseApplicationTownItemTableDef.LINDI_PURCHASE_APPLICATION_TOWN_ITEM;

/**
 * 林地指标交易平台_购买申请镇街购买项 服务层实现。
 *
 * @author 11987
 * @since 2025-07-01
 */
@Service
public class LindiPurchaseApplicationTownItemServiceImpl extends ServiceImpl<LindiPurchaseApplicationTownItemMapper, LindiPurchaseApplicationTownItem> implements LindiPurchaseApplicationTownItemService{

    @Override
    public List<LindiPurchaseApplicationTownItemVO> getTownItemListByPurchaseApplicationId(Long purchaseApplicationId){
        QueryWrapper queryWrapper = QueryWrapper.create()
                .where(LINDI_PURCHASE_APPLICATION_TOWN_ITEM.PURCHASE_APPLICATION_ID.eq(purchaseApplicationId));
        List<LindiPurchaseApplicationTownItem> townItemList = list(queryWrapper);
        return BeanUtil.copyToList(townItemList, LindiPurchaseApplicationTownItemVO.class);
    }

    @Override
    public void batchAddTownItems(List<LindiPurchaseApplicationTownItemVO> townItemList){
        System.out.println("townItemList:" + townItemList);
        if (townItemList != null && !townItemList.isEmpty()){
            List<LindiPurchaseApplicationTownItem> poList = BeanUtil.copyToList(townItemList
                    , LindiPurchaseApplicationTownItem.class);
            saveBatch(poList);
        }
    }

    @Override
    public void deleteTownItemsByPurchaseApplicationId(Long purchaseApplicationId){
        QueryWrapper queryWrapper = QueryWrapper.create()
                .where(LINDI_PURCHASE_APPLICATION_TOWN_ITEM.PURCHASE_APPLICATION_ID.eq(purchaseApplicationId));
        remove(queryWrapper);
    }
}
